﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Common;
using Common.Model;
namespace BLL
{
    //StockAlert
    public partial class StockAlertBLL
    {

        private readonly DAL.StockAlertDAL dal = new DAL.StockAlertDAL();
        public StockAlertBLL()
        { }

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Common.Model.StockAlert model)
        {
            return dal.Add(model);

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Common.Model.StockAlert model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除几条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Common.Model.StockAlert GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Common.Model.StockAlert GetModelByCache(int Id)
        {
            string CacheKey = "StockAlertModel-" + Id;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Common.Model.StockAlert)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Common.Model.StockAlert> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        public List<Common.Model.StockAlert> GetModelListByCache(string strWhere)
        {
            int Id = 1;
            string CacheKey = "StockAlertModelList-" + Id;
            object objModelList = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModelList == null)
            {
                try
                {
                    objModelList = GetModelList(strWhere);
                    if (objModelList != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModelList, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (List<Common.Model.StockAlert>) objModelList;
        }
        /// <summary>
        /// 返回真正需要的model，当然这是只有code是真实的
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Common.Model.StockAlert> GetUsedModelList(string strWhere)
        {
            DataSet ds = dal.GetUsedStockList(strWhere);
            return LazyDataTableToList(ds.Tables[0]);
        }
        public List<Common.Model.StockAlert> LazyDataTableToList(DataTable dt)
        {
            List<Common.Model.StockAlert> modelList = new List<Common.Model.StockAlert>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Common.Model.StockAlert model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Common.Model.StockAlert();
                    model.Code = dt.Rows[n]["Code"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得数据列表,DataTable转换为对象列表
        /// </summary>
        public List<Common.Model.StockAlert> DataTableToList(DataTable dt)
        {
            List<Common.Model.StockAlert> modelList = new List<Common.Model.StockAlert>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Common.Model.StockAlert model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Common.Model.StockAlert();
                    if (dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["CustomerId"].ToString() != "")
                    {
                        model.CustomerId = int.Parse(dt.Rows[n]["CustomerId"].ToString());
                    }
                    model.Code = dt.Rows[n]["Code"].ToString();
                    if (dt.Rows[n]["UpAlertPrice"].ToString() != "")
                    {
                        model.UpAlertPrice = decimal.Parse(dt.Rows[n]["UpAlertPrice"].ToString());
                    }
                    if (dt.Rows[n]["UpAlertMargin"].ToString() != "")
                    {
                        model.UpAlertMargin = decimal.Parse(dt.Rows[n]["UpAlertMargin"].ToString());
                    }
                    if (dt.Rows[n]["UpAuto"].ToString() != "")
                    {
                        model.UpAuto = decimal.Parse(dt.Rows[n]["UpAuto"].ToString());
                    }
                    if (dt.Rows[n]["UPChangeTime"].ToString() != "")
                    {
                        model.UPChangeTime = DateTime.Parse(dt.Rows[n]["UPChangeTime"].ToString());
                    }
                    if (dt.Rows[n]["DownAlertPrice"].ToString() != "")
                    {
                        model.DownAlertPrice = decimal.Parse(dt.Rows[n]["DownAlertPrice"].ToString());
                    }
                    if (dt.Rows[n]["DownAlertMargin"].ToString() != "")
                    {
                        model.DownAlertMargin = decimal.Parse(dt.Rows[n]["DownAlertMargin"].ToString());
                    }
                    if (dt.Rows[n]["DownAuto"].ToString() != "")
                    {
                        model.DownAuto = decimal.Parse(dt.Rows[n]["DownAuto"].ToString());
                    }
                    if (dt.Rows[n]["DownChangeTime"].ToString() != "")
                    {
                        model.DownChangeTime = DateTime.Parse(dt.Rows[n]["DownChangeTime"].ToString());
                    }
                    if (dt.Rows[n]["CreateTime"].ToString() != "")
                    {
                        model.CreateTime = DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得所有数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        #endregion

    }
}